package com.imay.live.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.imay.base.activity.BaseActivity;
import com.imay.live.helper.DialogFragmentHelper;
import com.imay.live.manager.EditTextManager;
import com.imay.live.proto.ErrorResult;
import com.imay.live.proto.ResultNoData;
import com.imay.live.proto.helper.ProtoCallBack;
import com.imay.live.proto.helper.ProtoHttpFactory;
import com.imay.live.proto.helper.ProtoRequestHelper;
import com.imay.live.utils.ToastUtil;
import com.imay.live.R;

import java.util.ArrayList;
import java.util.List;

import static com.imay.live.proto.ResultCode.CodeSuccess;

/**
 * created by nick
 * 我的-设置-帮助与反馈-反馈意见
 */
public class FeedbackActivity extends BaseActivity implements ProtoCallBack, View.OnClickListener {

    private String feedbackCommand;
    private EditText et_content_feedback;
    private TextView tv_feedback_edit_text_number;
    private EditText et_contact_feedback;
    private Button btn_confirm_feedback;

    public static void start(Context context) {
        Intent starter = new Intent(context, FeedbackActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected int getLayoutId() {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        return R.layout.activity_feedback;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setTitle(getString(R.string.setting_feedback_title));
        bindViews();
//        EditTextManager.getInstance().changeBgColor(btn_confirm_feedback, R.drawable.shape_corner100_bc, R.drawable.shape_corner100_9e, et_content_feedback, et_contact_feedback);


    }


    private void bindViews() {

        et_content_feedback = (EditText) findViewById(R.id.et_content_feedback);
        tv_feedback_edit_text_number = (TextView) findViewById(R.id.tv_feedback_edit_text_number);
        et_contact_feedback = (EditText) findViewById(R.id.et_contact_feedback);
        btn_confirm_feedback = (Button) findViewById(R.id.btn_confirm_feedback);

        btn_confirm_feedback.setOnClickListener(this);
        et_content_feedback.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                tv_feedback_edit_text_number.setText(300 - s.length() + "");
            }
        });
    }


    private void feedback() {
        DialogFragmentHelper.showLoadingDialog(getSupportFragmentManager(), "");
        feedbackCommand = ProtoRequestHelper.task(ProtoHttpFactory.feedback(et_content_feedback.getText().toString(), et_contact_feedback.getText().toString())).setTag(this.hashCode()).request(this);
    }

    @Override
    public void onRequestSuccess(String command, String data) {
        DialogFragmentHelper.closeLoadingDialog(getSupportFragmentManager());
        if (command.equals(feedbackCommand)) {
            ResultNoData result = JSON.parseObject(data, new TypeReference<ResultNoData>() {
            }.getType());
            if (result.result == CodeSuccess) {
                ToastUtil.show(this, R.string.success_feedback);
                finish();
            } else {
                ErrorResult.toastError(this, result.result);
            }
        }

    }

    @Override
    public void onRequestError(String command, int code, String message) {
        DialogFragmentHelper.closeLoadingDialog(getSupportFragmentManager());
        ErrorResult.toastError(this, code);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_confirm_feedback:
                if (TextUtils.isEmpty(et_content_feedback.getText())||TextUtils.isEmpty(et_contact_feedback.getText())) {
//                    ToastUtil.show(this, R.string.empty_content_feedback);
                    return;
                }
//                if (TextUtils.isEmpty(et_contact_feedback.getText())) {
//                    ToastUtil.show(this, R.string.empty_contact_feedback);
//                    return;
//                }
                feedback();
                break;
        }
    }
}
